home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_semaphores.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-13  |  7.8 KB  |  251 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28.  
  29.  
  30. #include "system_headers.h"
  31.  
  32. APTR BT_SemUpdate,BT_SemPrint,BT_SemObtain,BT_SemRelease,BT_SemRemove,BT_SemExit;
  33. APTR semtext,semlist;
  34.  
  35. APTR semcount;
  36. int  semcnt;
  37.  
  38. static APTR SemPool;
  39.  
  40. __asm __saveds LONG semlist_dspfunc(register __a2 char **array, register __a1 struct SemEntry *sementry, register __a0 struct Hook *hook)
  41. {
  42.         if (sementry) {
  43.                 *array++ = sementry->sm_address;
  44.                 *array++ = sementry->sm_name;
  45.                 *array++ = sementry->sm_nestcount;
  46.                 *array++ = sementry->sm_queuecount;
  47.                 *array++ = sementry->sm_owner;
  48.                 *array  = NULL;
  49.         } else {
  50.                 *array++ = ESC "bAddress";
  51.                 *array++ = ESC "bName";
  52.                 *array++ = ESC "bNest";
  53.                 *array++ = ESC "bQueue";
  54.                 *array++ = ESC "bOwner";
  55.                 *array   = NULL;
  56.         }
  57.         return(0);
  58. }
  59.  
  60. struct Hook semlist_dsphook = {
  61.  {NULL, NULL},
  62.  (ULONG (* )())semlist_dspfunc,
  63.  NULL, NULL
  64. };
  65.  
  66. __asm __saveds LONG semlist_cmpfunc(register __a1 struct SemEntry *sementry1, register __a2 struct SemEntry *sementry2)
  67. {
  68.     return strcmpi(sementry1->sm_name, sementry2->sm_name);
  69. }
  70.  
  71. struct Hook semlist_cmphook = {
  72.  {NULL, NULL},
  73.  (ULONG (* )())semlist_cmpfunc,
  74.  NULL, NULL
  75. };
  76.  
  77. void FreeSemaphores (void)
  78. {
  79.     MyFreePoolStructs(&SemPool, semtext, NULL, semlist);
  80. }
  81.  
  82. int GetSemaphores (struct SemEntry **first) {
  83.    struct SignalSemaphore  *sem;
  84.    struct SemEntry         *sementry,*previous = NULL;
  85.  
  86.    int semcnt = 0;
  87.    *first = 0;
  88.  
  89.    if (!SemPool) SemPool = tbCreatePool(MEMF_CLEAR, 4096, 4096);
  90.  
  91.    if (clientstate) {
  92.       if (SendDaemon ("GetSemList")) {
  93.          while ((sementry = tbAllocPooled(SemPool, sizeof(struct SemEntry))) \
  94.            && (ReceiveDecodedEntry ((UBYTE *) sementry, sizeof (struct SemEntry)))) {
  95.             IsHex (sementry->sm_address, (long *) &sementry->sm_adr);
  96.  
  97.             if (! *first)
  98.                *first = sementry;
  99.             if (previous)
  100.                previous->sm_next = sementry;
  101.  
  102.             semcnt++;
  103.             previous = sementry;
  104.          }
  105.       }
  106.    } else {
  107.       sem = FIRSTSEMAPHORE;
  108.    
  109.       while ((sem->ss_Link.ln_Succ != 0) && (sementry = tbAllocPooled(SemPool, sizeof(struct SemEntry)))) {
  110.          if (! *first)
  111.             *first = sementry;
  112.          if (previous)
  113.             previous->sm_next = sementry;
  114.    
  115.          sementry->sm_adr = sem;
  116.    
  117.                 _sprintf (sementry->sm_address, "$%08lx", sem);
  118.                 strncpy (sementry->sm_name, nonetest (sem->ss_Link.ln_Name), FILENAMELENGTH);
  119.                 _sprintf (sementry->sm_nestcount, "%3ld ", sem->ss_NestCount);
  120.          if (sem->ss_Owner) {
  121.                 _sprintf (sementry->sm_queuecount, "%3ld ", sem->ss_QueueCount);
  122.                 strncpy (sementry->sm_owner, nonetest (GetTaskName (sem->ss_Owner)), NODENAMELENGTH);
  123.          } else {
  124.                 strcpy (sementry->sm_queuecount, "--- ");
  125.                 strcpy (sementry->sm_owner, "-----");
  126.          }
  127.    
  128.          semcnt++;
  129.          previous = sementry;
  130.          sem = (struct SignalSemaphore *) sem->ss_Link.ln_Succ;
  131.       }
  132.    }
  133.    return (semcnt);
  134. }
  135.  
  136. void PrintSemaphores (char *filename) {
  137.    int   i=1;
  138.    BPTR  handle;
  139.    struct SemEntry *entryp = NULL;
  140.  
  141.    handle = HandlePrintStart (filename);
  142.    if ((handle) && (PrintOneLine (handle, "\n  Address  Name                           Nest Queue Owner\n\n"))) {
  143.       if (! WI_Semaphores) {
  144.          i = GetSemaphores (&entryp);
  145.       }
  146.       if (i) {
  147.          for (i=0;;i++) {
  148.             if (WI_Semaphores)
  149.                DoMethod (semlist,MUIM_List_GetEntry,i,&entryp);
  150.             if (!entryp) break;
  151.  
  152.             if (strcmp (entryp->sm_owner, "<none>"))
  153.                _sprintf (tmpstr2, " %s %-30s %4s %5s %s\n", entryp->sm_address, entryp->sm_name, entryp->sm_nestcount, entryp->sm_queuecount, entryp->sm_owner);
  154.             else
  155.                _sprintf (tmpstr2, " %s %-30s %4s  ---  -----\n", entryp->sm_address, entryp->sm_name, entryp->sm_nestcount);
  156.  
  157.             if (! (PrintOneLine (handle, tmpstr2)))
  158.                break;
  159.  
  160.             if (! WI_Semaphores)
  161.                entryp = entryp->sm_next;
  162.          }
  163.       }
  164.    }
  165.    HandlePrintStop();
  166. }
  167.  
  168. void ShowSemaphores (void) {
  169.    struct   SemEntry    *sem;
  170.  
  171.         ApplicationSleep();
  172.         set (semlist,MUIA_List_Quiet,TRUE);
  173.         set (BT_SemObtain, MUIA_Disabled, TRUE);
  174.         set (BT_SemRelease, MUIA_Disabled, TRUE);
  175.         set (BT_SemRemove, MUIA_Disabled, TRUE);
  176.  
  177.    FreeSemaphores();
  178.    semcnt = GetSemaphores (&sem);
  179.  
  180.    while (sem) {
  181.       InsertSortedEntry (semlist, (APTR *) &sem);
  182.       sem = sem->sm_next;
  183.    }
  184.  
  185.    SetCountText (semcount, semcnt);
  186.         AwakeApplication();
  187.         set (semlist,MUIA_List_Quiet,FALSE);
  188. }
  189.  
  190. void SendSemList (void) {
  191.    struct   SemEntry    *sem;
  192.  
  193.    FreeSemaphores();
  194.    GetSemaphores (&sem);
  195.  
  196.    while (sem) {
  197.       SendEncodedEntry ((UBYTE *) sem, sizeof (struct SemEntry));
  198.       sem = sem->sm_next;
  199.    }
  200.    FreeSemaphores();
  201. }
  202.  
  203.  
  204. char semaphores_title[WINDOWTITLELEN];
  205.  
  206. void SemaphoresWindow (BOOL state) {
  207.    if (state) {
  208.       if (WI_Semaphores) {
  209.          ShowSemaphores();
  210.       } else {
  211.          WI_Semaphores = WindowObject,
  212.          MUIA_Window_Title, MyGetWindowTitle (semaphores_title, "SEMAPHORES"),
  213.          MUIA_HelpNode, SemaphoresText,
  214.          MUIA_Window_ID, MakeListID('S','E','M','A'),
  215.          WindowContents, VGroup,
  216.             Child, semlist = MySortedListviewObject ("COL=0 DELTA=8,COL=1 DELTA=8,COL=2 DELTA=8 P=\33r,COL=3 DELTA=8 P=\33r,COL=4 DELTA=8",&semlist_dsphook, &semlist_cmphook),
  217.             Child, MyBelowListview (&semtext, &semcount),
  218.             Child, MyVSpace(2),
  219.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  220.                Child, BT_SemUpdate   = KeyButtonA (UpdateText ,ID_SEMUPDATE),
  221.                Child, BT_SemPrint    = KeyButtonA (PrintText  ,ID_SEMPRINT),
  222.                Child, BT_SemObtain   = KeyButtonA (ObtainText ,ID_SEMOBTAIN),
  223.                Child, BT_SemRelease  = KeyButtonA (ReleaseText,ID_SEMRELEASE),
  224.                Child, BT_SemRemove   = KeyButtonA (RemoveText ,ID_SEMREMOVE),
  225.                Child, BT_SemExit     = KeyButtonA (ExitText   ,ID_SEMEXIT),
  226.             End,
  227.          End, End;
  228.  
  229.          DoMethod (AP_Scout,OM_ADDMEMBER,WI_Semaphores);
  230.          DoMethod (WI_Semaphores,MUIM_Window_SetCycleChain,semlist,BT_SemUpdate,BT_SemPrint,BT_SemObtain,BT_SemRelease,BT_SemRemove,BT_SemExit,NULL);
  231.  
  232.          SetCloseRequest (WI_Semaphores,ID_SEMEXIT);
  233.          SetListActive (semlist,ID_SEMLV_ACTIVE);
  234.  
  235.          ShowSemaphores();
  236.  
  237.          SetWindowOpen (WI_Semaphores,semlist,ID_SEMEXIT);
  238.       }
  239.    } else if ((! state) && (WI_Semaphores)) {
  240.       SetWindowClose (WI_Semaphores,TRUE);
  241.  
  242.       FreeSemaphores();
  243.  
  244.       DoMethod (AP_Scout,OM_REMMEMBER,WI_Semaphores);
  245.       MUI_DisposeObject (WI_Semaphores);
  246.       WI_Semaphores = NULL;
  247.       semlist = NULL;
  248.    }
  249. }
  250.  
  251.